package network;

import com.jme3.app.SimpleApplication;
import com.jme3.network.Client;
import com.jme3.network.Network;
import com.jme3.system.JmeContext;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MainClient extends SimpleApplication {
    protected Client client;

    public static void main(String[] args) {
	MainClient app = new MainClient();
	app.start(JmeContext.Type.Display);
    }

    @Override
    public void simpleInitApp() {
	connectToServer();
	
    }

    protected void connectToServer() {
	try {
	    client = Network.connectToServer(NetworkUtil.HOST, NetworkUtil.PORT);
	    client.start();
	    ClientListener clientListener = new ClientListener();
	    client.addClientStateListener(clientListener);
	    client.addMessageListener(clientListener);
	    NetworkUtil.registerMessages();
	    PlayerInfo helloMessage = new PlayerInfo();
	    helloMessage.setPlayerName("Dan");
	    client.send(helloMessage);
	} catch (IOException ex) {
	    Logger.getLogger(MainClient.class.getName()).log(Level.SEVERE, "Unable to connect to server on port: "+NetworkUtil.PORT, ex);
	}
    }

    @Override
    public void destroy() {
	client.close();
	super.destroy();
    }
}
